﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

using AimGameLibrary.GameObject;
using AimGameLibrary.ScreenManager;
using System.Reflection;

namespace AimGameLibrary
{
    public class SettingEditorData
    {
        public String BuildOutput;
        public String MainScriptpath;
        public String EXEpath;
        public Point Resolution;

        public void SettingDefualt(String pathLocation)
        {
            BuildOutput = pathLocation;
            MainScriptpath = pathLocation;
            EXEpath = pathLocation + "\\AimGameStateManagement.exe";
            Resolution = ScreenResolution.GetResolution(ScreenResolution.HD720);
        }
    }

    public enum RunMode
    { 
        PreViewMode,
        GamePlayMode,
        NoneDeteleMode
    }

    public class Script
    {
        GameScreen gameScreen;
        ContentManager contentmanager;
        SpriteBatch inputspritebatch;

        AimGameControl inputaimgamecontrol;

        public const String SettingFile = "Setting.xml";

        public const String SubPart = "Data";
        public const String SubAudio = "Audios";
        public const String SubVideo = "Vedios";
        /// Data\SreenScript.xml
        public const String ScreenScriptFile = "SreenScript.xml";
        /// Data\[NameScreen]\MainScript.xml
        public const String MainScriptFile = "MainScript.xml";
        public const String DrawScripFile = "DrawScript.xml";
        public const String UpdateScriptFile = "UpdateScript.xml";

        public static SettingEditorData GetSettingFile()
        {
            XMLData<SettingEditorData> LoadSetting = new XMLData<SettingEditorData>();
            if (File.Exists(SettingFile))
                return LoadSetting.Load(SettingFile);
            else return null;
        }

        public Double[] GetFaceTimeScreen(String NameScreen)
        {
            SettingEditorData DataSetting = GetSettingFile();
            String getsubpart = System.IO.Path.Combine(DataSetting.BuildOutput, SubPart);
            
            Dictionary<String, BaseGameObject> dictionarybasegameobject = new Dictionary<string, BaseGameObject>();

            Precreateobject(LoadIdentification(System.IO.Path.Combine(getsubpart, ScreenScriptFile)), dictionarybasegameobject);

            if (dictionarybasegameobject.ContainsKey(NameScreen))
            {
                GameScreenData gamescreendata = (GameScreenData)dictionarybasegameobject[NameScreen];
                return new double[] { gamescreendata.FaceIn, gamescreendata.FaceOut };
            }
            return null;
        }
        
        #region LoadContent

        public void LoadContent(ContentManager contentmanager, SpriteBatch inputspritebatch)
        {
            this.contentmanager = contentmanager;
            this.inputspritebatch = inputspritebatch;
        }

        public void LoadContent(ContentManager contentmanager, SpriteBatch inputspritebatch, GameScreen gameScreen)
        {
            this.gameScreen = gameScreen;
            LoadContent(contentmanager, inputspritebatch);
        }
       
        #endregion

        public void RunLoadObject(String NameScreen,Dictionary<String, BaseGameObject> dictionarybasegameobject, List<IUpdateMainTime> UpdateGametime, List<IDraw> Draw,List<IHandleInputUpdate> HandleInput)
        {
            
            SettingEditorData DataSetting = GetSettingFile();
            
            String getsubpart = System.IO.Path.Combine(DataSetting.BuildOutput, SubPart);
            String FileScreen = System.IO.Path.Combine(getsubpart, ScreenScriptFile);
            

            if (System.IO.Directory.Exists(getsubpart))
            {
                #region Get Screen Load

                String TargetScreen = "";

                if (NameScreen == null)
                    TargetScreen = FindStartScreen(LoadIdentification(FileScreen));
                else TargetScreen = NameScreen;

                String partscreenTarget = System.IO.Path.Combine(getsubpart, TargetScreen);

                #endregion

                if (System.IO.Directory.Exists(partscreenTarget))
                {
                    #region SetFile Part

                    String FileMain = System.IO.Path.Combine(partscreenTarget, MainScriptFile);
                    String FileUpdate = System.IO.Path.Combine(partscreenTarget, UpdateScriptFile);
                    String FileDraw = System.IO.Path.Combine(partscreenTarget, DrawScripFile);

                    #endregion

                    if (File.Exists(FileMain) && File.Exists(FileUpdate) && File.Exists(FileDraw))
                    {
                        #region Load Object
                        
                            Precreateobject(LoadIdentification(FileMain), dictionarybasegameobject);

                            SetupToListManager(LoadIdentification(FileUpdate), dictionarybasegameobject, UpdateGametime);
                            SetupToListManager(LoadIdentification(FileDraw), dictionarybasegameobject, Draw);

                            SetupToListManager(LoadIdentification(FileMain), dictionarybasegameobject, HandleInput);

                            SetupParent(LoadIdentification(FileMain), dictionarybasegameobject);
                        
                        #endregion
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Not Get Subpart : " + getsubpart);
            }
        }

        public void RunLoadObject(String NameScreen, AimGameControl aimgamecontrol)
        {
            inputaimgamecontrol = aimgamecontrol;
            RunLoadObject(NameScreen, aimgamecontrol.DictionaryGameObjecT, aimgamecontrol.ListUpdateTimE, aimgamecontrol.ListDraW, aimgamecontrol.ListHandlE);
        }

        public String GetFindStartScreen()
        {
            SettingEditorData DataSetting = GetSettingFile();

            String getsubpart = System.IO.Path.Combine(DataSetting.BuildOutput, SubPart);
            String FileScreen = System.IO.Path.Combine(getsubpart, ScreenScriptFile);

            return FindStartScreen(LoadIdentification(FileScreen));
        }

        Identification LoadIdentification(String Path)
        {
            if (File.Exists(Path))
            {
                XMLData<Identification> loaddata = new XMLData<Identification>();
                return loaddata.Load(Path);
            }
            else return null;
        }

        String FindStartScreen(Identification identificationScreen)
        {
            Dictionary<String, BaseGameObject> dictionarybasegameobject = new Dictionary<string, BaseGameObject>();

            Precreateobject(identificationScreen, dictionarybasegameobject);

            String IsStartName = "";
            foreach (String key in dictionarybasegameobject.Keys)
            {
                if (dictionarybasegameobject[key].GetType() == typeof(GameScreenData))
                {
                    GameScreenData screendata = (GameScreenData)dictionarybasegameobject[key];
                    if (screendata.StartHere)
                    {
                        IsStartName = key;
                        break;
                    }
                }
            }
            return IsStartName;
        }

        #region LoadObject in Scene

        void Precreateobject(Identification getdata,Dictionary<String,BaseGameObject> DictionaryBaseGameObject)
        { 
            int countIndexNodeData = 0;
            if (getdata != null)
            {
                
                foreach (object obj in getdata.Items)
                {
                    if (obj.GetType() == typeof(AimGameLibrary.NodeData))
                    {
                        NodeData node = (NodeData)obj;
                        switch (node.MyType)
                        {
                            case NodeTypeValue.Screen:
                                GameScreenData gamescreendata = new GameScreenData();
                                gamescreendata.LoadData(getdata, countIndexNodeData,node);
                                DictionaryBaseGameObject.Add(node.MyName, gamescreendata);
                                break;
                            case NodeTypeValue.ScreenControl:
                                GameScreenControl gamescreencontrol = new GameScreenControl(this.gameScreen);
                                gamescreencontrol.LoadData(getdata, countIndexNodeData,node);
                                DictionaryBaseGameObject.Add(node.MyName, gamescreencontrol);
                                break;
                            case NodeTypeValue.Camera:
                                inputaimgamecontrol.CamerA.LoadData(getdata, countIndexNodeData, node);
                                break;
                            case NodeTypeValue.TackCamera:
                                GameTakeCamera gametakecamera = new GameTakeCamera();
                                gametakecamera.LoadData(getdata, countIndexNodeData,node);
                                gametakecamera.Listnameparent.Add(node.Data);
                                if (gameScreen != null)
                                    gametakecamera.OriginScreen = new Vector2(gameScreen.ScreenManager.GraphicsDevice.Viewport.Width / 2, gameScreen.ScreenManager.GraphicsDevice.Viewport.Height / 2);
                                else
                                {
                                    SettingEditorData DataSetting = GetSettingFile();
                                    gametakecamera.OriginScreen = new Vector2(DataSetting.Resolution.X/2, DataSetting.Resolution.Y/2);
                                    gametakecamera.spritebatch = this.inputspritebatch;
                                    gametakecamera.camera = this.inputaimgamecontrol.CamerA;
                                    String Filetakecamera = System.IO.Path.Combine(this.contentmanager.RootDirectory, "TempleGameScreen");
                                    Filetakecamera = System.IO.Path.Combine(Filetakecamera, "blank.xnb");
                                    if (File.Exists(Filetakecamera))
                                        gametakecamera.texture2d = this.contentmanager.Load<Texture2D>("TempleGameScreen/blank");
                                }
                                DictionaryBaseGameObject.Add(node.MyName, gametakecamera);
                                break;
                            case NodeTypeValue.Boolean:
                                GameBoolean gameboolean = new GameBoolean();
                                gameboolean.LoadData(getdata, countIndexNodeData,node);
                                DictionaryBaseGameObject.Add(node.MyName, gameboolean);
                                break;
                            case NodeTypeValue.Int:
                                #region Int
                                GameInt gameint = new GameInt();
                                gameint.LoadData(getdata, countIndexNodeData,node);
                                DictionaryBaseGameObject.Add(node.MyName, gameint);
                                #endregion
                                break;
                            case NodeTypeValue.Float:
                                #region Float
                                GameFloat gamefloat = new GameFloat();
                                gamefloat.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamefloat);
                                #endregion
                                break;
                            case NodeTypeValue.Vector2:
                                #region Vector2
                                GameVector2 gamevector2 = new GameVector2();
                                gamevector2.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamevector2);
                                #endregion
                                break;
                            case NodeTypeValue.Rectangle:
                                #region Rectangle
                                GameRectangle rectangle = new GameRectangle();
                                rectangle.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, rectangle);
                                #endregion
                                break;
                            case NodeTypeValue.String:
                                break;
                            case NodeTypeValue.Color:
                                GameColor gamecolor = new GameColor();
                                gamecolor.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamecolor);
                                break;
                            case NodeTypeValue.SpriteEffects:
                                GameSpriteEffects gamespriteeffects = new GameSpriteEffects();
                                gamespriteeffects.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamespriteeffects);
                                break;
                            case NodeTypeValue.Texture2D:
                                #region Texture2D
                                GameTexture2D texture2d = new GameTexture2D();
                                String TempFile = System.IO.Path.Combine(this.contentmanager.RootDirectory, node.MyName + ".xnb");
                                if (File.Exists(TempFile))
                                    texture2d.Texture2D = this.contentmanager.Load<Texture2D>(node.MyName);
                                DictionaryBaseGameObject.Add(node.MyName, texture2d);
                                #endregion
                                break;
                            case NodeTypeValue.SpriteFont:
                                break;
                            case NodeTypeValue.DrawSprite:
                                #region DrawSprite
                                Drawsprite drawsprite = new Drawsprite(this.inputspritebatch, this.inputaimgamecontrol.CamerA);
                                drawsprite.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, drawsprite);
                                #endregion
                                break;
                            case NodeTypeValue.DrawString:
                                break;
                            case NodeTypeValue.Animation2D:
                                #region Animation2D
                                GameAnimation2D animation2d = new GameAnimation2D();
                                animation2d.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, animation2d);
                                #endregion
                                break;
                            case NodeTypeValue.Body:
                                #region Body
                                GameBody body = new GameBody();
                                body.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, body);
                                #endregion
                                break;
                            case NodeTypeValue.Gravity:
                                GameGravity gravity = new GameGravity();
                                gravity.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gravity);
                                break;
                            case NodeTypeValue.Collision:
                                GameCollision collision = new GameCollision();
                                collision.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, collision);
                                break;
                            case NodeTypeValue.IrrklangPlayer:
                                GameIrrKlangPlayer irrklang = new GameIrrKlangPlayer(this.inputaimgamecontrol.SoundenginE);
                                irrklang.PathAudiO = this.contentmanager.RootDirectory;
                                irrklang.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, irrklang);
                                break;
                            case NodeTypeValue.VideoPlayer:
                                GameVideoPlayer vedioplayer = new GameVideoPlayer();
                                vedioplayer.PathVediO = this.contentmanager.RootDirectory;
                                vedioplayer.LoadData(getdata, countIndexNodeData, node);
                                if (File.Exists(vedioplayer.PathVediO))
                                    vedioplayer.VideO = this.contentmanager.Load<Video>(vedioplayer.PathFileVedio);
                                DictionaryBaseGameObject.Add(node.MyName, vedioplayer);
                                break;
                            case NodeTypeValue.KeyController:
                                GameKeyController gamekey = new GameKeyController();
                                gamekey.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamekey);
                                break;
                            case NodeTypeValue.ButtonController:
                                GameButtonController gamebutton = new GameButtonController();
                                gamebutton.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamebutton);
                                break;
                            case NodeTypeValue.AddValue:
                                GameAddValue gameaddvalue = new GameAddValue();
                                gameaddvalue.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gameaddvalue);
                                break;
                            case NodeTypeValue.SubtracValue:
                                GameSubtractValue gamesubtractvalue = new GameSubtractValue();
                                gamesubtractvalue.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamesubtractvalue);
                                break;
                            case NodeTypeValue.MultiplyValue:
                                GameMultiplyValue gamemultiplyvalue = new GameMultiplyValue();
                                gamemultiplyvalue.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamemultiplyvalue);
                                break;
                            case NodeTypeValue.DivisionValue:
                                GameDivisionValue gamedivisionvalue = new GameDivisionValue();
                                gamedivisionvalue.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gamedivisionvalue);
                                break;
                            case NodeTypeValue.RemainderValue:
                                GameRemainderValue gameremaindervalue = new GameRemainderValue();
                                gameremaindervalue.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gameremaindervalue);
                                break;
                            case NodeTypeValue.EqualsValue:
                                GameEqualsValue gameequalsvalue = new GameEqualsValue();
                                gameequalsvalue.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gameequalsvalue);
                                break;
                            case NodeTypeValue.IfElse:
                                GameIfelse gameifelse = new GameIfelse();
                                gameifelse.LoadData(getdata, countIndexNodeData, node);
                                DictionaryBaseGameObject.Add(node.MyName, gameifelse);
                                break;
                        }
                    }
                    countIndexNodeData++;
                }
            }
        }

        void SetupParent(Identification getdata, Dictionary<String, BaseGameObject> DictionaryBaseGameObject)
        {
            foreach (object obj in getdata.Items)
            {
                if (obj.GetType() == typeof(AimGameLibrary.NodeData))
                {
                    NodeData node = (NodeData)obj;
                    switch (node.MyType)
                    {
                        case NodeTypeValue.Camera:
                            if (inputaimgamecontrol.CamerA.InputMode == NodeInputMode.InputMode_1)
                                inputaimgamecontrol.CamerA.SetupParent(DictionaryBaseGameObject);
                            break;
                        
                        case NodeTypeValue.TackCamera:((GameTakeCamera)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        
                        case NodeTypeValue.Boolean:
                            GameBoolean gameboolean = (GameBoolean)DictionaryBaseGameObject[node.MyName];
                            if (gameboolean.InputMode == NodeInputMode.Boolean_1)
                                gameboolean.SetupParent(DictionaryBaseGameObject);
                            break;
                        case NodeTypeValue.Int:
                            GameInt gameint = (GameInt)DictionaryBaseGameObject[node.MyName];
                            if (gameint.InputMode == NodeInputMode.Int_1)
                                gameint.SetupParent(DictionaryBaseGameObject);
                            break;
                        case NodeTypeValue.Float:
                            GameFloat gamefloat = (GameFloat)DictionaryBaseGameObject[node.MyName];
                            if (gamefloat.InputMode == NodeInputMode.Float_1)
                                gamefloat.SetupParent(DictionaryBaseGameObject);
                            break;
                        case NodeTypeValue.Vector2:
                            GameVector2 gamevector2 = (GameVector2)DictionaryBaseGameObject[node.MyName];
                            if (gamevector2.InputMode == NodeInputMode.Float_2 || gamevector2.InputMode == NodeInputMode.Vector2_1)
                                gamevector2.SetupParent(DictionaryBaseGameObject);
                            break;
                        case NodeTypeValue.Rectangle:
                            GameRectangle gamerectangle = (GameRectangle)DictionaryBaseGameObject[node.MyName];
                            if (gamerectangle.InputMode == NodeInputMode.Int_4 || gamerectangle.InputMode == NodeInputMode.Rectangle_1)
                                gamerectangle.SetupParent(DictionaryBaseGameObject);
                            break;
                        case NodeTypeValue.String:
                            break;
                        case NodeTypeValue.Color:
                            break;
                        case NodeTypeValue.SpriteEffects:
                            GameSpriteEffects gamespriteeffects = (GameSpriteEffects)DictionaryBaseGameObject[node.MyName];
                            if (gamespriteeffects.InputMode == NodeInputMode.SpriteEffects_1)
                                gamespriteeffects.SetupParent(DictionaryBaseGameObject);
                            break;
                        
                        case NodeTypeValue.Texture2D:
                            break;
                        case NodeTypeValue.SpriteFont:
                            break;
                        case NodeTypeValue.DrawSprite: ((Drawsprite)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        
                        case NodeTypeValue.DrawString:
                            break;
                        case NodeTypeValue.Animation2D:
                            GameAnimation2D animation2d = (GameAnimation2D)DictionaryBaseGameObject[node.MyName];
                            if (animation2d.InputMode == NodeInputMode.Int_1)
                                animation2d.SetupParent(DictionaryBaseGameObject);
                            break;
                        case NodeTypeValue.Body:
                            GameBody gamebody = (GameBody)DictionaryBaseGameObject[node.MyName];
                            if (gamebody.InputMode == NodeInputMode.Body_1 || gamebody.InputMode == NodeInputMode.Float_5)
                                gamebody.SetupParent(DictionaryBaseGameObject);
                            break;

                        case NodeTypeValue.Gravity: ((GameGravity)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        case NodeTypeValue.Collision: ((GameCollision)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        
                        case NodeTypeValue.KeyController:
                            GameKeyController gamekey = (GameKeyController)DictionaryBaseGameObject[node.MyName];
                            if (gamekey.InputMode == NodeInputMode.Pressed_Release)
                            {
                                gamekey.SetupParent(DictionaryBaseGameObject, gamekey.ListNamePressed, gamekey.ListObjectPressed);
                                gamekey.SetupParent(DictionaryBaseGameObject, gamekey.ListNameRelease, gamekey.ListObjectRelease);
                            }
                            else if (gamekey.InputMode == NodeInputMode.Isdown_Isup)
                            {
                                gamekey.SetupParent(DictionaryBaseGameObject, gamekey.ListNameIsdown, gamekey.ListObjectIsdown);
                                gamekey.SetupParent(DictionaryBaseGameObject, gamekey.ListNameIsup, gamekey.ListObjectIsup);
                            }
                            break;
                        case NodeTypeValue.ButtonController:
                            GameButtonController gamebutton = (GameButtonController)DictionaryBaseGameObject[node.MyName];
                            if (gamebutton.InputMode == NodeInputMode.Pressed_Release)
                            {
                                gamebutton.SetupParent(DictionaryBaseGameObject, gamebutton.ListNamePressed, gamebutton.ListObjectPressed);
                                gamebutton.SetupParent(DictionaryBaseGameObject, gamebutton.ListNameRelease, gamebutton.ListObjectRelease);
                            }
                            else if (gamebutton.InputMode == NodeInputMode.Isdown_Isup)
                            {
                                gamebutton.SetupParent(DictionaryBaseGameObject, gamebutton.ListNameIsdown, gamebutton.ListObjectIsdown);
                                gamebutton.SetupParent(DictionaryBaseGameObject, gamebutton.ListNameIsup, gamebutton.ListObjectIsup);
                            }
                            break;

                        case NodeTypeValue.AddValue: ((GameAddValue)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        case NodeTypeValue.SubtracValue: ((GameSubtractValue)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        case NodeTypeValue.MultiplyValue: ((GameMultiplyValue)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        case NodeTypeValue.DivisionValue: ((GameDivisionValue)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        case NodeTypeValue.RemainderValue: ((GameRemainderValue)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        case NodeTypeValue.EqualsValue: ((GameEqualsValue)DictionaryBaseGameObject[node.MyName]).SetupParent(DictionaryBaseGameObject); break;
                        
                        case NodeTypeValue.IfElse:
                            GameIfelse gameifelse = (GameIfelse)DictionaryBaseGameObject[node.MyName];
                            gameifelse.SetupParent(DictionaryBaseGameObject);
                            gameifelse.SetupParent(DictionaryBaseGameObject, gameifelse.ListNameIf, gameifelse.ListObjectUpdateIf);
                            gameifelse.SetupParent(DictionaryBaseGameObject, gameifelse.ListNameElse, gameifelse.ListObjectUpdateElse);
                            break;
                    }
                }
            }
        }
        
        #region Setup List Manager

        /// Draw
        void SetupToListManager(Identification identification,Dictionary<String, BaseGameObject> dictionarybasegameobject, List<IDraw> Draw)
        { 
            if(identification.Items != null)
            foreach (object obj in identification.Items)
            {
                if(obj.GetType() == typeof(AimGameLibrary.NodeData))
                {
                    NodeData node = (NodeData)obj;
                    if (dictionarybasegameobject.ContainsKey(node.MyName))
                        Draw.Add(dictionarybasegameobject[node.MyName]);
                }
            }
        }

        /// UpdateGameMaintime
        void SetupToListManager(Identification identification, Dictionary<String, BaseGameObject> dictionarybasegameobject, List<IUpdateMainTime> UpdateGametime)
        {
            if (identification.Items != null)
            foreach (object obj in identification.Items)
            {
                if (obj.GetType() == typeof(AimGameLibrary.NodeData))
                {
                    NodeData node = (NodeData)obj;
                    if(dictionarybasegameobject.ContainsKey(node.MyName))
                        UpdateGametime.Add(dictionarybasegameobject[node.MyName]);
                }
            }
        }

        /// IHandleInputUpdate
        void SetupToListManager(Identification identification, Dictionary<String, BaseGameObject> dictionarybasegameobject, List<IHandleInputUpdate> HandleInputUpdate)
        {
            if (identification.Items != null)
                foreach (object obj in identification.Items)
                {
                    if (obj.GetType() == typeof(AimGameLibrary.NodeData))
                    {
                        NodeData node = (NodeData)obj;
                        if (dictionarybasegameobject.ContainsKey(node.MyName))
                            if (node.MyType == NodeTypeValue.KeyController || node.MyType == NodeTypeValue.ButtonController)
                            {
                                GameController key = (GameController)dictionarybasegameobject[node.MyName];
                                HandleInputUpdate.Add(key);
                            }                        
                    }
                }
        }

        #endregion

        
        /// โหลดข้อมูลจาก mainscript สร้าง obj ทุกตัว แล้วเก็บไว้ใน DictionaryBaseGameObject *
        /// โหลดข้อมูลจาก update ,draw ว่าตัวไหนอยู่ในไหนบาง แล้วเอาจาก DictionaryBaseGameObject มาใส่
        /// update มีแค่ update ต้องเพิ่ม updatetime เข้าไปด้วย
        /// setup obj ทุกตัวจาก DictionaryBaseGameObject ว่ามีการรับ obj อะไรมาบ้าง
        /// 
        #endregion

        public static String GetName(String inputString, int count, int Getindex)
        {
            int[] subname = new int[count];
            string[] name = new string[count];
            if (inputString != "NULL")
            {
                string tempinput = inputString;

                for (int i = 0; i < count - 1; i++)
                {
                    subname[i] = tempinput.IndexOf("|");
                    name[i] = tempinput.Substring(0, subname[i]);
                    tempinput = tempinput.Remove(0, (name[i].Length + 1));
                }
                name[count - 1] = tempinput;

                return name[Getindex];
            }
            return "NULL";
        }

        /// สร้าง Intiallize Screen เพื่อให้การทำงานของ Screen management ทำงานได้
        /// Screen ต่อ ๆ ไปจะทำการสร้างตามที่กำหนดใน Screen นั้นๆ

    }
}
